import request from './request.js'
class api {
  constructor() {
    this._baseUrl = 'https://czxxc.unicloudcar.com/'
    this._loginUrl = 'http://testclientapi.unicloudcar.com/'
    this._request = new request
    this._request.setErrorHandler(this.errorHander)
  }

  /**
   * 统一的异常处理方法
   */
  errorHander(err) {
    console.error(err)
    // if (err.errMsg === "request:fail timeout") {
    //   wx.showModal({
    //     title: '接口响应超时',
    //     content: '接口响应超时，请稍后再试！',
    //     confirm: '确定',
    //     showCancel: false,
    //   })
    // }
    // wx.hideLoading()
  }
  getHeader () {
    // if (wx.getStorageSync('token') ) {
    //   return {'Authorization': `Bearer ${wx.getStorageSync('token')}`}
    // } else {
    //   return ''
    // }
  }

  getHeader1 () {
    return {'content-type': 'application/json'}
  }

  /**
   * 使用code登录
   */
  weChatLogin (data){
    return this._request.postRequest(this._baseUrl + 'weixin/wxauth', data, this.getHeader()).then(res => res.data)
  }
  /**
   * 获取用户手机号 iv
   */
  weChatBinding (data){
    return this._request.postRequest(this._baseUrl + 'weixin/wxbing', data, this.getHeader()).then(res => res.data)
  }
  /**
   * 发送验证码
   * post
   * cellphone string
   */
  getValidCode (data){
    return this._request.postRequest(this._loginUrl + 'security/sendValidCode', data, this.getHeader()).then(res => res.data)
  }
  /**
   * 注册
   * post
   * cellphone   string
   * validCode  string
   */
  loginWithValidCode (data){
    return this._request.postRequest(this._loginUrl + 'security/login', data, this.getHeader1()).then(res => res.data)
  }
  /**
   * 获取洗车机状态
   * post
   * deviceid   string
   */
  getWashDeviceState (data){
    return this._request.postRequest(this._baseUrl + 'wash/getstatus', data, this.getHeader()).then(res => res.data)
  }
  /**
   * begin washing
   * post
   * tradno   string
   */
  startWach (data){
    return this._request.postRequest(this._baseUrl + 'wash/startwash', data, this.getHeader()).then(res => res.data)
  }
  /**
   * start wash
   * post
   * tradno   string
   */
  stopWash (data){
    return this._request.postRequest(this._baseUrl + 'wash/stopwash', data, this.getHeader()).then(res => res.data)
  }
  /**
   * get wash mode
   * post
   * tradno   string
   */
  getWashMode (data){
    return this._request.getRequest(this._baseUrl + 'wash/getwashoption', data, this.getHeader()).then(res => res.data)
  }
  /**
   * create Order
   * post
   * washid string
   * deviceid string
   * washtype 洗车类型 ordinarywash/finewas
   *
   */
  createOrder (data){
    return this._request.postRequest(this._baseUrl + 'wash/createorder', data, this.getHeader()).then(res => res.data)
  }
  /**
   * pay
   * post
   * tradno  orderNum
   * channel 1
   */
  pay (data){
    return this._request.postRequest(this._baseUrl + 'wash/wxpay', data, this.getHeader()).then(res => res.data)
  }
  /**
   * getOrderList
   * post
   * page
   * pagesize 1
   */
  getOrderList (data){
    return this._request.postRequest(this._baseUrl + 'wash/getorderlist', data, this.getHeader()).then(res => res.data)
  }
  /**
   * order detail
   * post
   * tradno orderNum
   */
  getOrderDetail (data){
    return this._request.postRequest(this._baseUrl + 'wash/getorderone', data, this.getHeader()).then(res => res.data)
  }

}
export default api

